package com.niit.zerotestpdfreader;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.niit.zerotestpdfreader.entity.Recent;
import com.niit.zerotestpdfreader.service.RecentService;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity implements View.OnClickListener {

//    Bitmap[] bitmaps;
    private String[] permissions = {Manifest.permission.READ_EXTERNAL_STORAGE};
    private AlertDialog dialog;
    private Button buttonFindByPath;
    private Button openFileExplorer;
    private Button recentReadings;
    private Button loginActivate;
    private TextView textView;
    private String filePath = "";

    SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent intent = new Intent(this, PdfPage.class);

        textView = findViewById(R.id.pathFind);
        buttonFindByPath = findViewById(R.id.buttonFind);
        buttonFindByPath.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String path = textView.getText().toString();
                Toast toast = Toast.makeText(MainActivity.this, path, Toast.LENGTH_LONG);
                intent.putExtra("path", path);
                startActivity(intent);
//                readPDF(path);
            }
        });


        openFileExplorer = findViewById(R.id.buttonFindFile);
        openFileExplorer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                chooseFile();
//                Intent date = onActivityResult(Intent date);
//                Toast toast = Toast.makeText(MainActivity.this, filePath, Toast.LENGTH_LONG);
//                intent.putExtra("path", filePath);
//                startActivity(intent);
            }
        });


        Intent recentInten = new Intent(this, RecentReading.class);

        recentReadings = findViewById(R.id.RecentReadings);
        recentReadings.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                List<Recent> recents = new ArrayList<Recent>();;

                RecentService recentService = new RecentService(MainActivity.this);
                Boolean sta = recentService.selectRecent(recents);

                if (sta) {
//                    Bundle bundle = new Bundle();
//                    bundle.putSerializable("list", (Serializable) recents);
                    recentInten.putExtra("list", (Serializable) recents);
                    startActivity(recentInten);
                }
            }
        });

        Intent intentLogin = new Intent(this, LoginActivity.class);

        loginActivate = findViewById(R.id.LoginActivate);
        loginActivate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(intentLogin);
            }
        });

    }

    @Override
    public void onClick(View v) {

//        chooseFile();
    }


    /**
     * 打开本地文件器
     */
    private static final int FILE_SELECT_CODE = 0;
    private static final String TAG = "VideoActivity";
    private void chooseFile() {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("*/*");
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        try {
            startActivityForResult(Intent.createChooser(intent, "选择文件"), FILE_SELECT_CODE);
//			Toast.makeText(this, "亲，来了啊-_-!!", Toast.LENGTH_SHORT).show();
        } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(this, "亲，木有文件管理器啊-_-!!", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        // TODO Auto-generated method stub
        if (resultCode != Activity.RESULT_OK) {
            Log.e(TAG, "onActivityResult() error, resultCode: " + resultCode);
            super.onActivityResult(requestCode, resultCode, data);
            return;
        }
        if (requestCode == FILE_SELECT_CODE) {
            Uri uri = data.getData();
            filePath = "/mnt/sdcard/" + uri.getPath().split(":")[1];
            Log.i(TAG, "path : " + uri.getPath());

            Intent intent = new Intent(this, PdfPage.class);

            intent.putExtra("path", filePath);
            startActivity(intent);
        }

        super.onActivityResult(requestCode, resultCode, data);
    }
}